package com.my.main.service.userAndRight.serviceImpl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.common.utils.IdWorker;
import com.my.main.entity.userAndRight.SysRightsRoleRouters;
import com.my.main.mapper.userAndRight.SysRightsRoleRoutersMapper;
import com.my.main.service.userAndRight.SysRightsRoleRoutersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysRightsRoleRoutersServiceImpl extends BaseServiceImpl<SysRightsRoleRouters>  implements SysRightsRoleRoutersService {

    @Autowired
    private SysRightsRoleRoutersMapper sysRightsRoleRoutersMapper;
    @Autowired
    private IdWorker idWorker;

    @Resource(name= "sysRightsRoleRoutersMapper")
    public void setBaseMapper(BaseMapper<SysRightsRoleRouters> sysRightsRoleRoutersMapper) {
        super.setBaseMapper(sysRightsRoleRoutersMapper);
    }

    @Override
    public void isUpdateSysRightsRoleRouters(String roleId, List<String> ridList, String ip, String userId) {
        try {

            // 删除所有
            sysRightsRoleRoutersMapper.isDeleteByRoleId(roleId);

            // 添加
            for(String rid : ridList) {
                SysRightsRoleRouters srrr = new SysRightsRoleRouters();
                srrr.setRelationId(idWorker.nextId() + "");
                srrr.setRoleId(roleId);
                srrr.setRouterId(rid);
                srrr.setAddIp(ip);
                srrr.setAddUserId(userId);
                srrr.setLastEditBy(userId);
                srrr.setLastEditIp(ip);
                srrr.setIsDel(false);

                sysRightsRoleRoutersMapper.isSave(srrr);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
